import {
  myRequesy
} from '../../request/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {}, // 用户登录状态
    userState: {}, //保存用户转态信息
    Hireveal: false, //控制修改封面事件
    coverShow: true, //控制编辑用户信息事件
    shut: true, //控制新人专享
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserinfo()
  },



  // 获取我的用户信息
  getUserinfo() {
    let that = this
    myRequesy({
      url: 'user/info',
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          id: res.data.data.id,
          nickName: res.data.data.nickName,
          avatarUrl: res.data.data.avatarUrl,
          signature: res.data.data.signature,
          coverImg: res.data.data.coverImg
        })
      }
    })
  },

  // 获取用户状态信息
  getUserConut() {
    let that = this
    myRequesy({
      url: 'user/info-count'
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          userState: res.data.data
        })
      }

    })
  },

  //用户编辑背景图底部弹框事件
  AddShow() {
    this.setData({
      Hireveal: true
    })
    wx.hideTabBar()
  },

  // 用户点击/取消遮罩层隐藏底部弹窗事件 
  btnReveal() {
    wx.hideTabBar()

    this.setData({
      Hireveal: false
    })
  },

  // 修改用户封面事件回调 
  uploadimage() {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0]
        let filename = src
        let fileNames = filename.lastIndexOf("."); //取到文件名开始到最后一个点的长度
        let fileNameLength = filename.length; //取到文件名长度
        let fileFormat = filename.substring(fileNames + 1, fileNameLength); //截
        if (fileFormat === "jpg" || fileFormat === "png" || fileFormat === "jpeg") {
          //  获取裁剪图片资源后，跳转到裁剪页并携带图片
          wx.navigateTo({
            url: '/pages/upload/index?src=' + src,
          })
          return
        }
        wx.showToast({
          title: '文件格式错误',
          icon: "none"
        })
      }
    })

  },


  // 用户预览封面事件回调
  btnPreview() {
    wx.previewImage({
      urls: [this.data.coverImg],
      showmenu: true,
      success: (res) => {
        console.log(res);

      }
    })
  },

  // 修改用户昵称
  headleNickName(e) {
    this.setData({
      nickName: e.detail.value
    })
  },

  // 同步微信信息
  headleUpdateWx() {
    let that = this
    myRequesy({
      url: 'user/wechat-info'
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          avatarUrl: res.data.data.avatarUrl,
          nickName: res.data.data.nickName
        })
      }
    })
  },

  // 修改用户个性签名
  headleSignature(e) {
    this.setData({
      signature: e.detail.value
    })

  },

  //编辑用户信息回调
  btnCover() {
    this.setData({
      coverShow: false
    })
    wx.hideTabBar()
  },

  //头像修改
  headleUserPhoto() {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const src = res.tempFilePaths[0]
        let filename = src.lastIndexOf(".")
        let fileNameLength = filename.length
        let fileFormat = src.substring(filename + 1, fileNameLength)
        console.log(fileFormat);
        if (fileFormat === "jpg" || fileFormat === "png" || fileFormat === "jpeg") {
          //  获取裁剪图片资源后，跳转到裁剪页并携带图片
          wx.navigateTo({
            url: '/pages/tailoring/tailoring?src=' + src,
          })
          return
        }
        wx.showToast({
          title: '文件格式错误',
          icon: "none"
        })
      }
    })
  },

  // 跳转到用户动态
  navDyn() {
    let token = wx.getStorageSync('userInfo').token
    if (!token) {
      this.getLogin()
      return
    }
    wx.navigateTo({
      url: '/pages/dynamic/dynamic?id=' + this.data.id,
    })

  },
  navAtt() {
    let token = wx.getStorageSync('userInfo').token
    if (!token) {
      this.getLogin()
      return
    }
    wx.navigateTo({
      url: '/pages/attention/attention',
    })
  },
  navAtts() {
    let token = wx.getStorageSync('userInfo').token
    if (!token) {
      this.getLogin()
      return
    }
    wx.navigateTo({
      url: '/pages/attention/attention?status=true',
    })
  },
  navComm() {
    let token = wx.getStorageSync('userInfo').token
    if (!token) {
      this.getLogin()
      return
    }
    wx.navigateTo({
      url: '/pages/comment/comment',
    })
  },

  navPra() {
    let token = wx.getStorageSync('userInfo').token
    if (!token) {
      this.getLogin()
      return
    }
    wx.navigateTo({
      url: '/pages/praise/praise',
    })
  },

  navRecord() {
    let token = wx.getStorageSync('userInfo').token
    if (!token) {
      this.getLogin()
      return
    }
    wx.navigateTo({
      url: '/pages/record/record',
    })
  },


  //保存编辑
  headleSave() {
    let {
      nickName,
      avatarUrl,
      signature,
      coverImg
    } = this.data
    if (signature.length > 30) {
      wx.showToast({
        title: '文字最多只能编写30个字',
        icon: "none"
      })
      return
    }
    if (nickName.length > 10) {
      wx.showToast({
        title: '昵称最多只能编写10个字',
        icon: "none"
      })
      return
    }
    myRequesy({
      url: 'user/update-info',
      method: 'POST',
      data: {
        coverImg,
        avatarUrl,
        nickName,
        signature
      }
    }).then(res => {
      if (res.data.code === 0) {
        this.getUserinfo()
        this.setData({
          coverShow: true
        })
        wx.showTabBar()
      } else if (res.data.code === 3015) {
        wx.showToast({
          title: '昵称和签名不能包含敏感词',
          icon: "none"
        })
      }
    })

  },

  // 取消修改
  headleCancel() {
    this.setData({
      coverShow: true
    })
    this.getUserinfo()
    wx.showTabBar()
  },

  headleUseHead() {
    let token = wx.getStorageSync('userInfo').token
    if (!token) {
      this.getLogin()
    }
  },

  //获取用户信息
  getLogin() {
    let that = this
    wx.getUserProfile({
      desc: '获取您的用户名和头像',
      success(res1) {
        console.log(res1);
        wx.login({
          success(res2) {
            console.log(res2);
            let userData = {
              code: res2.code,
              nickName: res1.userInfo.nickName,
              avatarUrl: res1.userInfo.avatarUrl
            }
            myRequesy({
              url: 'Login',
              data: userData,
              method: 'POST'
            }).then(result => {
              wx.setStorage({
                data: result.data.data,
                key: 'userInfo',
                success() {
                  that.getUserinfo()
                  that.getUserConut()
                  wx.setStorageSync('state', true)
                }
              })

            })
          }
        })
      }
    })
  },
  // 跳转到新人用户页
  headleTask() {
    let token = wx.getStorageSync('userInfo').token
    if (token) {
      wx.navigateTo({
        url: '/pages/mission/mission',
      })
      return
    }
    this.getLogin()

  },

  // 切换tab时获取用户信息
  onTabItemTap() {
    let token = wx.getStorageSync('userInfo').token
    if (!token) {
      this.getLogin()
    }
  },



  // 跳转到用户反馈页
  headleFeedback() {
    let token = wx.getStorageSync('userInfo').token
    if (!token) {
      this.getLogin()
      return
    }
    wx.navigateTo({
      url: '/pages/feedback/feedback',
    })
  },


  // 商务合作
  headleBusiness() {
    let token = wx.getStorageSync('userInfo').token
    if (!token) {
      this.getLogin()
      return
    }
    myRequesy({
      url: 'business/qrcode'
    }).then(res => {
      if (res.data.code === 0) {
        wx.previewImage({
          urls: [res.data.data.url],
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.coverShow) {
      this.getUserinfo()
    }

    this.getUserConut()

    // 新人专享
    if (wx.getStorageSync('conceal')) {
      this.setData({
        shut: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})